#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "log.h"

#define LOG_FILE "./log"

#define oops(msg) {perror(msg); exit(1);}

#define PORTNUM 13000

#define MSG_LEN 1024

int main(int argc, char **argv)
{
	struct sockaddr_in addr;
	int sock_fd;
	char message[MSG_LEN] = {0};

	write_log(LOG_DEBUG, LOG_FILE, "%s", "start!");

	if((sock_fd=socket(AF_INET, SOCK_STREAM, 0))==-1)
	{
		write_log(LOG_DEBUG, LOG_FILE, "%s", "create socket error!");
		oops("create socket error!");
	}

	memset((void*)&addr, 0, sizeof(addr));

	addr.sin_port = htons(PORTNUM);
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr("192.168.1.102");

	if(connect(sock_fd, (struct sockaddr*)&addr, sizeof(addr))==-1)
	{
		write_log(LOG_DEBUG, LOG_FILE, "%s", "connect faild!");
		oops("connect failed!");
	}

	int re = 0;

	while(1)
	{
		re = read(sock_fd, (void*)message, MSG_LEN);
		if(re==-1)
		{
			write_log(LOG_DEBUG, LOG_FILE, "%s", "read failed!");
			oops("read failed!");
		}

		printf( "%s", message);

	}

	close(sock_fd);
	return 0;
}
